package com.qkl.xiatang.app.utils.appenum;

import com.qkl.xiatang.app.utils.AppUtils;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 感应项：1水温，2盐度，3 PH，4溶氧，5亚盐，6氨氮
 */
@Getter
public enum SensorTypeEnum {
    A1(1,"水温",3),
    A2(2,"盐度",2),
    A3(3,"PH",1),
    A4(4,"溶氧",4),
    A5(5,"亚盐",6),
    A6(6,"氨氮",5),
    ;

    private SensorTypeEnum(Integer type,String name,Integer typeNormal){
        this.type = type;
        this.name = name;
        this.typeNormal = typeNormal;
        EnumUtil.typeMap.put(type,this);
        EnumUtil.typeNormalMap.put(type,SensorTypeNormalEnum.getEnum(typeNormal));
    }

    private Integer type;

    private String name;

    /**
     * 传感器正常值类型映射
     */
    private Integer typeNormal;

    public static SensorTypeNormalEnum getNormalEnum(Integer type){
        return EnumUtil.typeNormalMap.get(type);
    }

    public static String getTypeName(Integer type){
        return EnumUtil.typeMap.get(type).getName();
    }

    public static List<Map<String,Object>> toTypeList(String sensorId ,String sensorType){
        List<Map<String,Object>> list = new ArrayList<>();
        Collection<SensorTypeEnum> collection = EnumUtil.typeMap.values();
        if(StringUtils.isNotEmpty(sensorType)) {
            for (SensorTypeEnum typeEnum : collection) {
                if(inType(sensorType,typeEnum.getType())){
                    Map<String, Object> map = new HashMap<>();
                    map.put("type", typeEnum.getType());
                    map.put("typeName", typeEnum.getName());
                    map.put("id", sensorId + "-" + typeEnum.getType());
                    list.add(map);
                }
            }
        }
        return list;
    }

    private static boolean inType(String sensorType,int type){
        return sensorType.indexOf(String.valueOf(type)) > -1;
    }


    private static  class EnumUtil{
        public static Map<Integer,SensorTypeEnum> typeMap = new HashMap<>();
        public static Map<Integer,SensorTypeNormalEnum> typeNormalMap = new HashMap<>();
    }
}
